package com.bwie.controller;

import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.ExtUserInfo;
import com.alipay.api.domain.InvoiceKeyInfo;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.domain.InvoiceInfo;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.SubMerchant;

import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("pay")
@CrossOrigin
public class ZFBController {
    @SneakyThrows
    @GetMapping("pay")
    private void pay(HttpServletResponse resp) throws AlipayApiException {
// 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        // 设置商户订单号
        model.setOutTradeNo("20150320010111199");

        // 设置订单总金额
        model.setTotalAmount("8899.88");

        // 设置订单标题
        model.setSubject("Iphone6 16G");

        // 设置产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }


        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(pageRedirectionData);
        resp.getWriter().flush();
        resp.getWriter().close();
    }
    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCc9XNM8Ane1ssJxbWseYm26bepwbDybaoZjkpGGxtJ4o4ncag9wWLR8SHJKS2NyrB5z1rWoJJrOSqbsFUNvSRXpHPJdnMd4024QHq5izrYBOGdk1C1+0OovJoT0HEv0wDpuNeg1UE3sY1F0vWNa/wKInsA68b8dDIhmPVMG3H1qH+cIMOm1t+cpD9KFs2iZfBswftW/5AoP8mx+g333kP33J75KBC9wBwudjzaXaYLI1iw7YENIl4O2pXkevS3tsaRNZ1ZPjIT5FY4dMnsCMFnZx6noTJLgX4WykiUhOX1zTCfu/qna6cIYmuJ15phX2qJ29pjw7Olgsh8TQGVADenAgMBAAECggEADLoSSVDFXB5KhFZVrtCEDCLTU55y/JVLz5qsBxc3lTwbjrM2kxtVKfTFaow+N1sSCC56DSEj/jG6dLlNjDmj8r9Q7CsAlpHRwXjo+paBhPaQXLiPONJQ4CO60pR6OQiA8HKeeVoRWbS54cmDQ55X53J4StDTZ7D8R6EhcrRe/y4zBFAbp/k73L9jR+ejuPt4tvIfEYa0yy06YnqM9p5V32gzWuOofap1mpM/uAlksBa8YvMH4Fspb/bOkVw/uCY+57iSspsGZIfR7iMaRqG1NVPOMAUx0iP7BqsS3+HM/2qQm2QF04q6VvlmhuB+q89siBBs1uZMs0IsxdWK62+cgQKBgQD/SHpywLlBarezuoel/1tYgT0hBKAugCMlfCrs9jP+/D1xLnq/kHVomivcz3y86jGTx57SS+wgjRCWuKEdNDeHYQaHI+eRZtWbEfRwGvSvzaxxfL8X23dO53AXp+Q4Qobr0mypq2zoSw60hukzjlZG+GerYO6f5EzJdwc8jUAOYQKBgQCdZkmIfQ1Xw7yP1Y+O5LGte2yrIKrOus12pq9093xiMGrrvW2o+9GgKciUGwL7v8F5rYCl6443k1AW3XrKGhe3h7G3WAmYs09b9Zo/uAdDEvvtojGAsj3FdWS3F8J0aQKhZVGR8doKrVh1xQabapao2Agh8Knulc/EoevW53KzBwKBgQDQSXKe7mA437nDCw5iglclf01BA2Uui54hl43uiXMjOOtozWOZqWQiVKg1IZXFDdAlo1oSe5/Nb0t5rmrGSTSFYMTSvpjSKfVe8OdgEJ5UUpvAfOftr0NJPcxXgMTUonMzNh0y0oupfJQi93COgDQk88iiLBSGoN+GAXgGD7mUAQKBgG8PtMUVD+wklhGbrE80eVai51uckkmKr92P4fATjPWvmlI6+HyrKw/rS7eNk75VYMxdq4/ntRLr5DAuGS0v2hpLKg/nk+XmaBHILA3X6W/fnlnf/zQDEOkaW3WB01jWm9+MIVFsqOCtzeRToMTbIfGDdcGVEiLiHJdvdXzPFTTTAoGADXwLnpPd9LjK31YgMipzGpx29perMiF3KqRd12YAZXYhCybhN4uD7Yx6ZUPGrgxsdh+rTP+GNc5VBWXt0xKy7N8AHOJJ2Iydh8j3eRhRXZUozVJyscdHME/PeK08GldmZ6AFJ6i/HiOz6X9A2Dcto3c4qpsRHVtXON6RhGoAWJc=";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnPVzTPAJ3tbLCcW1rHmJtum3qcGw8m2qGY5KRhsbSeKOJ3GoPcFi0fEhySktjcqwec9a1qCSazkqm7BVDb0kV6RzyXZzHeNNuEB6uYs62AThnZNQtftDqLyaE9BxL9MA6bjXoNVBN7GNRdL1jWv8CiJ7AOvG/HQyIZj1TBtx9ah/nCDDptbfnKQ/ShbNomXwbMH7Vv+QKD/JsfoN995D99ye+SgQvcAcLnY82l2mCyNYsO2BDSJeDtqV5Hr0t7bGkTWdWT4yE+RWOHTJ7AjBZ2cep6EyS4F+FspIlITl9c0wn7v6p2unCGJrideaYV9qidvaY8OzpYLIfE0BlQA3pwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000145623496");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}
